(ns server
  (:import (com.sun.net.httpserver HttpHandler HttpServer)
	   (java.net InetSocketAddress))
  (:use clojure-web.html))

;;(defn send-response-headers [{:response}]

(defn test-fn [request]
  (let [exchange (:exchange request)
	is (.getRequestBody exchange)
	res (with-out-str
	      (html
		(:html
		 (:body
		  (:h1 "FOO")
		  (:pre (print-safe request))))))]
    (.read is)
    (.sendResponseHeaders exchange 200
			  (.length res))
    (with-open [os (.getResponseBody exchange)]
      (.write os (.getBytes res)))))

(defn make-handler [f]
  (proxy [HttpHandler] []
    (handle [exchange] (f {:exchange exchange}))))

;(defn make-dir-handler
;  ([] (make-dir-handler nil))
;  ([path]
;     (make-handler
;      (fn [request]

;{:uri-mapping ["/" default-handler
;               "/foo" foo-handler

;  If there is already running one on PORT, kill it before
;  starting the new one."

(defn http-server
  "Start an http-server listenting on PORT."

  ([] (http-server 80 15))
  ([port] (http-server 15))
  ([port backlog]
     (doto (.create HttpServer (new InetSocketAddress port)
		    backlog)
       (createContext "/" (make-handler test-fn))
       (setExecutor nil)
       (start))))











     
     



    



	

